package org.linlinjava.litemall.admin.controller;

import java.util.List;

import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallComment;
import org.linlinjava.litemall.db.service.LitemallCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理评论控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/comment")
@Validated
public class AdminCommentController {

    /**
     * 评论服务
     */
    @Autowired
    private LitemallCommentService commentService;

    /**
     * 列表
     *
     * @param userId  用户id
     * @param valueId id值
     * @param page    页面
     * @param limit   限制
     * @param sort    排序
     * @param order   订单
     * @return {@link Object}
     */
    @GetMapping("/list")
    @LogAnno
    public Object list(String userId, String valueId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallComment> commentList = commentService.querySelective(userId, valueId, page, limit, sort, order);
        return ResponseUtil.okList(commentList);
    }

    /**
     * 删除
     *
     * @param comment 评论
     * @return {@link Object}
     */
    @PostMapping("/delete")
    @LogAnno
    public Object delete(@RequestBody LitemallComment comment) {
        Integer id = comment.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        commentService.deleteById(id);
        return ResponseUtil.ok();
    }

}
